# Maintainer: Zach Bacon <zachbacon@budgiemedia.rocks>

_realname=vapoursynth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=49
pkgrel=1
pkgdesc="A video processing framework with simplicity in mind (mingw-w64)"
arch=('any')
url="http://www.vapoursynth.com/"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cython"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-imagemagick"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-tesseract-ocr"
         "${MINGW_PACKAGE_PREFIX}-zimg")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=(!strip staticlibs)
source=("vapoursynth-${pkgver}.tar.gz::https://github.com/vapoursynth/vapoursynth/archive/R${pkgver}.tar.gz")
sha256sums=('126d1e68d3a3e80d1e215c8a2a5dc8773f5fcac70a6c22dadc837bccb603bccd')

prepare() {
  cd "${srcdir}/${_realname}-R${pkgver}"
  sed -i 's/inline __forceinline/__forceinline/g' src/core/genericfilters.cpp
  autoreconf -vfi
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  ../${_realname}-R${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-python-module=yes

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-R${pkgver}"/COPYING.LGPLv2.1 ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LGPLv2.1

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  local _py3ver=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/vapoursynth/*.dll.a
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/python${_py3ver}/site-packages/*.dll.a
  mv -f ${pkgdir}${MINGW_PREFIX}/lib/bin/*.dll ${pkgdir}${MINGW_PREFIX}/lib/vapoursynth/
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/bin
}
